package com.bestv.handler;

import com.bestv.db.DbFactory;
import com.bestv.db.DbHelper;
import com.bestv.db.MysqlDb;
import com.bestv.db.OracleDb;
import com.bestv.util.DbConstant;
import org.apache.log4j.Logger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by charle-chen on 15/11/25.
 */
public class UpdateInfoThread extends Thread {
    private int maxCount;
    private int start;
    private int offset=0;
    private int limit=2000;
    private Logger logger = Logger.getLogger(getClass());

    @Override
    public void run() {

            MysqlDb mysqlDb = (MysqlDb)DbFactory.getDbInstance(DbConstant.MYSQL_DB);
            OracleDb oracleDb = (OracleDb)DbFactory.getDbInstance(DbConstant.ORACLE_DB);

            do {
                try {
                    String sql = "select name,code,p_type_id from info where update_flag<>2 limit " + offset + ", " + limit;
                    Statement st = mysqlDb.getConn().createStatement();
                    ResultSet rs = st.executeQuery(sql);
                    ResultSet oracleRs = null;
                    String oracleSql = "";
                    String updateSql = "";
                    while (rs.next()) {
                        switch (rs.getInt("p_type_id")) {
                            case 2:
                                oracleSql = "select status from vas_movie_view_for_se where code = '" + rs.getString("code") + "'";
                                break;
                            case 3:
                                oracleSql = "select status from vas_series_view_for_se where code = '" + rs.getString("code") + "'";
                                break;
                            default:
                                oracleSql = "select status from vas_program_view_for_se where code = '" + rs.getString("code") + "'";
                                break;
                        }

                        Statement oracleSt = oracleDb.getConn().createStatement();
                        oracleRs = oracleSt.executeQuery(oracleSql);
                        while (oracleRs.next()) {
                            if (oracleRs.getInt("status") == 0) {
                                updateSql = "update info set update_flag=2,status=0,update_time_for_index = now() where code='" + rs.getString("code") + "'";
                                logger.info(Thread.currentThread().getName() + ": update info set code = '" + rs.getString("code") + "';name = '" + rs.getString("name") + "'");
                                mysqlDb.getConn().createStatement().executeUpdate(updateSql);
                            }
                        }
                        oracleRs.close();
                        oracleSt.close();
                    }

                    rs.close();
                    st.close();
                    offset+=limit;
                    logger.info("the current offset = " + offset);
                    logger.info("the maxCount = " + maxCount);
                } catch (SQLException e) {
                    logger.error("sql execute error");
                    e.printStackTrace();
                    return;
                }
            } while (offset < start+maxCount);

            logger.info(Thread.currentThread().getName() +": update info table finished");

            DbHelper.closeDb(mysqlDb);
            DbHelper.closeDb(oracleDb);
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public void setStart(int start) {
        this.start = start;
        this.offset = start;
    }
}


